package cn.com.shopec.erp.product.commo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.product.entity.DefectReplce;
import cn.com.shopec.erp.product.vo.DefectReplceVo;

/**
 * 
 * @ClassName: DefectiveConvertor
 * @Description: 次品退换货对象转换器
 * @author: LiangShunWang
 * @date 2016-11-21 下午17:16:22
 * 
 */
@Component
public class DefectReplceVoConvertor {

	public List<DefectReplceVo> convert2DefectiveVos(List<DefectReplce> defectiveList) {
		List<DefectReplceVo> defectiveVos = null;

		if (LogicUtil.isNotNullAndEmpty(defectiveList)) {
			defectiveVos = new ArrayList<DefectReplceVo>();

			for (DefectReplce defective : defectiveList) {
				DefectReplceVo brandVo = this.convert2DefectiveVo(defective);
				defectiveVos.add(brandVo);
			}
		}

		return defectiveVos;
	}

	public PageFinder<DefectReplceVo> convert2PagedDefectiveVos(PageFinder<DefectReplce> pagedBrands) {
		PageFinder<DefectReplceVo> pagedBrandVos = null;

		if (LogicUtil.isNotNull(pagedBrands.getData())) {
			List<DefectReplceVo> defectiveVos = this.convert2DefectiveVos(pagedBrands.getData());

			pagedBrandVos = new PageFinder<DefectReplceVo>(pagedBrands.getPageNo(), pagedBrands.getPageSize(),
					pagedBrands.getRowCount(), defectiveVos);
		}

		return pagedBrandVos;
	}

	public DefectReplceVo convert2DefectiveVo(DefectReplce defective) {
		DefectReplceVo defectiveVo = null;
		if (LogicUtil.isNotNull(defective)) {
			defectiveVo = new DefectReplceVo();
			defectiveVo.setDefectReplceId(defective.getDefectReplceId());
			defectiveVo.setDefectReplceName(defective.getDefectReplceName());
			defectiveVo.setReplceType(defective.getReplceType());
			defectiveVo.setProcessState(defective.getProcessState());
			defectiveVo.setSupCode(defective.getSupCode());
			defectiveVo.setSupName(defective.getSupName());
			defectiveVo.setAuditor(defective.getAuditor());
			defectiveVo.setCreateTime(defective.getCreateTime());
			defectiveVo.setInitiater(defective.getInitiater());
			defectiveVo.setInitiateTime(defective.getInitiateTime());
			defectiveVo.setProDesc(defective.getProDesc());
			defectiveVo.setDeleteFlag(defective.getDeleteFlag());
			defectiveVo.setWhNo(defective.getWhNo());
			defectiveVo.setWhName(defective.getWhName());
			defectiveVo.setCommoNo(defective.getCommoNo());
			defectiveVo.setCommoName(defective.getCommoName());
			defectiveVo.setProdBarCode(defective.getProdBarCode());
			defectiveVo.setSpecification(defective.getSpecification());
			defectiveVo.setDefectiveNumber(defective.getDefectiveNumber());
			defectiveVo.setProdNo(defective.getProdNo());
			defectiveVo.setPoNo(defective.getPoNo());
			defectiveVo.setDescription(defective.getDescription());
			defectiveVo.setSpecName(defective.getSpecName());
			defectiveVo.setDefectiveReason(defective.getDefectiveReason());
		}
		return defectiveVo;
	}

	public DefectReplce convert2Defective(DefectReplceVo defectiveVo) {
		DefectReplce defective = null;

		if (LogicUtil.isNotNull(defectiveVo)) {
			defective = new DefectReplce();
			defective.setDefectReplceId(defectiveVo.getDefectReplceId());
			defective.setDefectReplceName(defectiveVo.getDefectReplceName());
			defective.setReplceType(defectiveVo.getReplceType());
			defective.setProcessState(defectiveVo.getProcessState());
			defective.setSupCode(defectiveVo.getSupCode());
			defective.setSupName(defectiveVo.getSupName());
			defective.setAuditor(defectiveVo.getAuditor());
			defective.setCreateTime(defectiveVo.getCreateTime());
			defective.setInitiater(defectiveVo.getInitiater());
			defective.setInitiateTime(defectiveVo.getInitiateTime());
			defective.setProDesc(defectiveVo.getProDesc());
			defective.setDeleteFlag(defectiveVo.getDeleteFlag());
			if (defectiveVo.getDeleteFlag() == null) {
				defective.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			} else {
				defective.setDeleteFlag(defectiveVo.getDeleteFlag());
			}
		}

		return defective;
	}

	public DefectReplce convert2Defective(DefectReplceVo defectiveVo, DefectReplce defective) {
		if (LogicUtil.isNotNull(defectiveVo) && LogicUtil.isNotNull(defective)) {
			defective.setDefectReplceId(defectiveVo.getDefectReplceId());
			defective.setDefectReplceName(defectiveVo.getDefectReplceName());
			defective.setReplceType(defectiveVo.getReplceType());
			defective.setProcessState(defectiveVo.getProcessState());
			defective.setSupCode(defectiveVo.getSupCode());
			defective.setSupName(defectiveVo.getSupName());
			defective.setAuditor(defectiveVo.getAuditor());
			defective.setCreateTime(defectiveVo.getCreateTime());
			defective.setInitiater(defectiveVo.getInitiater());
			defective.setInitiateTime(defectiveVo.getInitiateTime());
			defective.setProDesc(defectiveVo.getProDesc());
			defective.setDeleteFlag(defectiveVo.getDeleteFlag());
			return defective;
		} else {
			return null;
		}
	}

	/***
	 * 公共转换出入库状态
	 * 
	 * @param defectReplceId
	 * @param purchaseOrder
	 * @param status
	 * @return
	 */
	public DefectReplce convert2DefectiveUpdateAuditResult(String defectReplceId, DefectReplce defect, String status)
			throws Exception {
		if (LogicUtil.isNotNull(defectReplceId) && LogicUtil.isNotNull(defect) && (status != null)) {
			defect.setProcessState(status);
			if (status.equals(ProductSCMConstant.DEFECTREPLCE_STATE_2)
					|| status.equals(ProductSCMConstant.DEFECTREPLCE_STATE_5)) {
				defect.setStorageTime(DateUtil.formatTimesTampDate(new Date()));// 修改完成时间
			}
			if (status.equals(ProductSCMConstant.DEFECTREPLCE_STATE_2)
					&& defect.getReplceType().equals("" + ProductSCMConstant.REPLCETYPE_REPLACEMENT)) {
				defect.setProcessState(ProductSCMConstant.DEFECTREPLCE_STATE_3);// 当出库完成后判断是换货还是退货，如果是换货则需要进行后续流程
			}
			defect.setOutWarehouseTime(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
			return defect;
		} else {
			return null;
		}
	}

}
